package com.googlecode.c4e;

import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.continuum.Activator;
import org.apache.continuum.internal.ConnectionProfileManager;
import org.apache.continuum.model.ConnectionProfile;
import org.apache.maven.continuum.xmlrpc.client.ContinuumXmlRpcClient;
import org.eclipse.jface.resource.ImageDescriptor;

public class ContinuumHelper {

  /**
   * TODO move this to place where it might be nullified.
   */
  public static Map<Integer, ImageDescriptor> imageDescriptors = initializeImageDescriptors();

  public static ContinuumXmlRpcClient createClient(String profileId) throws Exception {
    List<ConnectionProfile> profiles = ConnectionProfileManager.loadConnectionProfiles();
    for (ConnectionProfile profile : profiles) {
      if (profile.getId().equals(profileId)) {
        return new ContinuumXmlRpcClient(new URL(profile.getConnectionUrl()), profile.getUsername(), profile.getPassword());
      }
    }
    throw new RuntimeException("Unknow profile " + profileId);
  }


  private static Map<Integer, ImageDescriptor> initializeImageDescriptors() {
    Map<Integer, ImageDescriptor> result = new HashMap<Integer, ImageDescriptor>();
    result.put(2, Activator.getDefault().getImageDescriptor("icons/success.gif"));
    result.put(3, Activator.getDefault().getImageDescriptor("icons/warning.gif"));
    result.put(4, Activator.getDefault().getImageDescriptor("icons/error.gif"));
    result.put(6, Activator.getDefault().getImageDescriptor("icons/building.gif"));
    return Collections.unmodifiableMap(result);
  }
}
